import time

from selenium.webdriver.common.by import By

from base.ApplicationXpath import ApplicationBase
from base.ObjectMap import ObjectMap
from common.tools import get_file_path


class ApplicationPage(ApplicationBase, ObjectMap):
    def common_click_button(self, driver, button_name):
        button_xpath = self.common_button(button_name)
        return self.element_click(driver, By.XPATH, button_xpath)

    def common_input_value(self, driver, label_name, input_value):
        input_xpath = self.label_input(label_name)
        return self.element_fill_value(driver, By.XPATH, input_xpath, input_value)

    def click_combobox(self, driver, label_name):
        combobox_xpath = self.combobox(label_name)
        return self.element_click(driver, By.XPATH, combobox_xpath)

    def select_combobox_detail(self, driver, item):
        item_xpath = self.combobox_detail(item)
        return self.element_click(driver, By.XPATH, item_xpath)

    def click_active_button(self, driver, button_name):
        button_xpath = self.active_button(button_name)
        return self.element_click(driver, By.XPATH, button_xpath)

    def upload_file(self, driver, file, suffix):
        file_path = get_file_path(file)
        input_xpath = self.file_input(suffix, multiple=False)
        return self.upload(driver, By.XPATH, input_xpath, file_path)

    def does_direct_app_publish(self, driver, app_name):
        name_xpath = self.published_app(app_name)
        return self.element_is_display(driver, By.XPATH, name_xpath)

    def create_application(self, driver, file_name, model_name, app_name):
        if self.does_direct_app_publish(driver, app_name):
            return True
        self.common_click_button(driver, "Create")
        self.upload_file(driver, file_name, ".apk, .zip")
        time.sleep(3)
        self.click_combobox(driver, "Model")
        self.select_combobox_detail(driver, model_name)
        self.click_active_button(driver, "Next")
        time.sleep(1)
        self.click_active_button(driver, "Next")
        self.common_input_value(driver, "Name", app_name)
        self.click_active_button(driver, "Submit")
        return True
